# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import datetime
import os
import sys

class JdPipeline(object):
    keys = ['面膜', '洗面奶', '花露水', '化妆棉', '眼霜', '杏仁', '饼干', '薯片', '麦片', '牛肉干', '巧克力', '红酒',
            '伏特加', '金酒', '茅台', '啤酒', '牛奶', '鸡蛋', '猪肋骨', '鱼', '土鸡', '牛排', '苹果', '橙子', '速冻水饺',
            'NFC果汁', '玉米油', '米', '盐', '糖', '酱油', '醋', '卷纸', '抽纸', '沐浴露', '洗发水', '洗洁精', '洗衣粉',
            '香皂', '保鲜膜', '保鲜袋', '卫生巾', '牙膏', '牙刷', '毛巾']
    res = [0] * len(keys)
    # debug = []

    def process_item(self, item, spider):
        index = self.keys.index(item['category'])
        self.res[index] += item['comment_number_detail']
        # self.res[index] += item['comment_number']
        # self.debug.append([item['comment_number_detail'], item['comment_number'], item['goods_page'], item['goods_id']])
        return item

    def close_spider(self, spider):
        reload(sys)
        sys.setdefaultencoding('gbk')
        keys = [u'面膜', u'洗面奶', u'花露水', u'化妆棉', u'眼霜', u'杏仁', u'饼干', u'薯片', u'麦片', u'牛肉干', u'巧克力', u'红酒',
                u'伏特加', u'金酒', u'茅台', u'啤酒', u'牛奶', u'鸡蛋', u'猪肋骨', u'鱼', u'土鸡', u'牛排', u'苹果', u'橙子', u'速冻水饺',
                u'NFC果汁', u'玉米油', u'米', u'盐', u'糖', u'酱油', u'醋', u'卷纸', u'抽纸', u'沐浴露', u'洗发水', u'洗洁精', u'洗衣粉',
                u'香皂', u'保鲜膜', u'保鲜袋', u'卫生巾', u'牙膏', u'牙刷', u'毛巾']
        date_now = datetime.datetime.now().strftime('../data/JD_%Y%m%d_%H%M%S.csv')
        file_out = open(date_now, 'w')
        file_out.write(','.join(keys) + '\n')
        file_out.write(','.join([str(i) for i in self.res]) + '\n')
        file_out.close()
        print os.path
        # # print self.debug
        # file_debug = open('../data/debug.csv', 'w')
        # for i in self.debug:
        #     file_debug.write(','.join([str(ii) for ii in i]) + '\n')
        # file_debug.close()
